package cn.supermarket.service.impl;

import cn.supermarket.ex.ServiceException;
import cn.supermarket.mapper.PmsInboundMapper;
import cn.supermarket.pojo.dto.AddProductInputDTO;
import cn.supermarket.pojo.dto.UpdateProductInputDTO;
import cn.supermarket.pojo.entity.PmsInbound;
import cn.supermarket.pojo.entity.ProductOutput;
import cn.supermarket.pojo.vo.ProductInputVO;
import cn.supermarket.pojo.vo.ProductOutputVO;
import cn.supermarket.service.iservice.IPmsInboundService;
import cn.supermarket.service.iservice.IStockService;
import cn.supermarket.utils.SnowFlakeGenerateIdWorker;
import cn.supermarket.web.State;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 入库单 服务实现类
 * </p>
 *
 * @author the shy
 * @since 2022-07-14
 */
@Service
public class PmsInboundServiceImpl implements IPmsInboundService {

    @Autowired
    private PmsInboundMapper pmsInboundMapper;

    @Autowired
    private IStockService stockService;

    @Transactional
    @Override
    public void addInboundRecord(AddProductInputDTO addProductInputDTO) {
        // 实例化ProductOutput对象
        PmsInbound pmsInbound = new PmsInbound();
        // 复制同名属性
        BeanUtils.copyProperties(addProductInputDTO, pmsInbound);
        // 获得当前时间
        LocalDateTime now = LocalDateTime.now();
        // 雪花算法生成随机id
        SnowFlakeGenerateIdWorker snowFlakeGenerateIdWorker = new SnowFlakeGenerateIdWorker(0L, 0L);
        Long orderNo = snowFlakeGenerateIdWorker.nextId();
        // 补全其他信息
        pmsInbound.setIsEnable(0);
        pmsInbound.setIsDisplay(1);
        pmsInbound.setIsCancel(0);
        pmsInbound.setGmtCreate(now);
        pmsInbound.setGmtModify(now);
        pmsInbound.setInOrderNo(orderNo);
        // 执行添加
        pmsInboundMapper.insertAddInboundRecord(pmsInbound);
        // 调用库存方法增加库存
        stockService.addProductCount(pmsInbound.getProductId(),pmsInbound.getQuantity());

    }

    @Override
    public void updateInfo(UpdateProductInputDTO updateProductInputDTO) {

        // 通过用户输入的id查询
        List<ProductInputVO> list = pmsInboundMapper.getListByInOrderNo(updateProductInputDTO.getInOrderNo());
        // 判断结果是否为空
        if (list.isEmpty()) {
            // 如果为空,抛出异常
            throw new ServiceException(State.ERR_NOT_FOUND, "单号错误");
        }
        // 设置最后修改信息为现在
        updateProductInputDTO.setGmtModify(LocalDateTime.now());
        // 执行修改
        pmsInboundMapper.updateInfoByInOrderNo(updateProductInputDTO);

    }

    @Override
    public List<ProductInputVO> getListByInOrderNo(Long inOrderNo) {
        // 通过单号查询出库记录列表
        List<ProductInputVO> list = pmsInboundMapper.getListByInOrderNo(inOrderNo);
        // 判断是否为空
        if (list.isEmpty()) {
            // 如果为空,抛出异常
            throw new ServiceException(State.ERR_NOT_FOUND, "单号错误");
        }
        // 返回查询到的结果
        return list;
    }

    @Override
    public List<ProductInputVO> getListByDate(Date date) {
        // 通过日期查询出库记录列表
        List<ProductInputVO> list = pmsInboundMapper.getListByDate(date);
        // 判断是否为空
        if (list.isEmpty()) {
            // 如果为空,抛出异常
            throw new ServiceException(State.ERR_NOT_FOUND, "当前日期暂无数据");
        }
        // 返回查询到的结果
        return list;
    }
}
